/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hcht.xrlh.zfryk.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.hcht.xrlh.zfryk.entity.DrZfryk;
import com.hcht.xrlh.zfryk.service.DrZfrykService;

/**
 * 执法人员库Controller
 * @author lrz
 * @version 2016-12-19
 */
@Controller
@RequestMapping(value = "${adminPath}/zfryk/drZfryk")
public class DrZfrykController extends BaseController {

	@Autowired
	private DrZfrykService drZfrykService;
	
	@Autowired
	private SystemService systemService;
	
	@ModelAttribute
	public DrZfryk get(@RequestParam(required=false) String id) {
		DrZfryk entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drZfrykService.get(id);
		}
		if (entity == null){
			entity = new DrZfryk();
		}
		return entity;
	}
	
	@RequiresPermissions("zfryk:drZfryk:view")
	@RequestMapping(value = {"list", ""})
	public String list(DrZfryk drZfryk, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrZfryk> page = drZfrykService.findPage(new Page<DrZfryk>(request, response), drZfryk); 
		model.addAttribute("page", page);
		return "xrlh/zfryk/drZfrykList";
	}

	@RequiresPermissions("zfryk:drZfryk:view")
	@RequestMapping(value = "form")
	public String form(DrZfryk drZfryk, Model model) {
		model.addAttribute("drZfryk", drZfryk);
		return "xrlh/zfryk/drZfrykForm";
	}

	@RequiresPermissions("zfryk:drZfryk:edit")
	@RequestMapping(value = "save")
	public String save(DrZfryk drZfryk, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, drZfryk)){
			return form(drZfryk, model);
		}
		//删除原有人员库
		DrZfryk para = new DrZfryk();
        para.setSxId(drZfryk.getSxId());
        List<DrZfryk> rylist = drZfrykService.findList(para);
        for(DrZfryk ry:rylist){
        	drZfrykService.deleteById(ry);
        }
		String[] userids = request.getParameter("userids").split(",");
		List<DrZfryk> list = new ArrayList<DrZfryk>();
		for(int j=0;j<userids.length;j++){
			DrZfryk ry = new DrZfryk(); 
			ry.setSxId(drZfryk.getSxId());
			ry.setRyFlag(drZfryk.getRyFlag());
			ry.setUser(systemService.getUser(userids[j]));
			list.add(ry);
		}
		drZfrykService.saveList(list);
		addMessage(redirectAttributes, "保存执法人员库成功");
		//return "redirect:"+Global.getAdminPath()+"/zfryk/drZfryk/?repage";
		return "redirect:"+Global.getAdminPath()+"/zfdxktitle/drZfdxkTitle/form?id="+drZfryk.getSxId();
	}
	
	@RequiresPermissions("zfryk:drZfryk:edit")
	@RequestMapping(value = "delete")
	public String delete(DrZfryk drZfryk, RedirectAttributes redirectAttributes) {
		drZfrykService.delete(drZfryk);
		addMessage(redirectAttributes, "删除执法人员库成功");
		return "redirect:"+Global.getAdminPath()+"/zfryk/drZfryk/?repage";
	}

}